/**
 * this is the standard round cuttout class
 * it will make the ends of the shapes
 * @author James Metting
 *
 */

import java.util.Random;

import javax.media.opengl.GL;

public class RoundCut extends PuzzleCut{
	double size;
	float displacement;
	/*public static void main(String[] args)
	{
		//tests the range of the size
		for (int i = 0; i < 10; i++)
		{
			Random rand = new Random();
			//between PuzzlePiece.size/10
			//and PuzzlePiece.size*.3
			double lower = (PuzzlePiece.size/10);
			double upper = (PuzzlePiece.size*.5);
			double size = rand.nextDouble()*(upper - lower)+lower;
			System.out.println("Size = "+size);
		}
	}*/
	public RoundCut()
	{
		Random rand = new Random();
		//between PuzzlePiece.size/10
		//and PuzzlePiece.size*.3
		double lower = PuzzlePiece.size/3;
		double upper = PuzzlePiece.size*.6;
		size = rand.nextDouble()*(upper - lower)+lower;
		displacement=(float) (rand.nextDouble()*(size/2));
		//randomize invert
		double inv = rand.nextDouble();
		inverted = true;
		if (inv>=.5)
			inverted = false;
	}
	public RoundCut(double size, float displacement, boolean inverted)
	{
		this.size = size;
		this.displacement = displacement;
		this.inverted = inverted;
	}
	/**
	 * the color of the piece and the background color have to be specified,
	 * the piece will know which one to use by the way it was setup.
	 * @param gl
	 * @param color
	 * @param backColor
	 * @param Coordinate location of cut
	 * @param Coordinate location of piece
	 */
	public void display(GL gl,PuzzleColor color,PuzzleColor backColor,Coordinate coord,Coordinate location,int direction)
	{
		float disx = 0;
		float disy =0;
		//displacement!! figure out an easier way?
		if(inverted)
		{
			switch(direction){
			case 0:disy = 0 - displacement;break;
			case 1:disy = displacement;break;
			case 2:disx = displacement;break;
			case 3:disx = 0- displacement;break;
			}
			gl.glColor3f(backColor.getRed(), backColor.getGreen(), backColor.getBlue());
		}
		else
		{
			switch(direction){
			case 0:disy = displacement;break;
			case 1:disy = 0 - displacement;break;
			case 2:disx = 0 - displacement;break;
			case 3:disx = displacement;break;
			}
			gl.glColor3f(color.getRed(), color.getGreen(), color.getBlue());
		}
		float radius = (float) size/2;
		double TWO_PI =  2 * Math.PI;
		double segment = ((size*Math.PI)/60);
		gl.glBegin (GL.GL_POLYGON);

		if(!inverted)
		{
			for (double theta = 0; theta < TWO_PI; theta += segment) {
		    	float x = (float) (coord.x + (Math.sin(theta) * radius)+disx);
		    	float y = (float) (coord.y + (Math.cos(theta) * radius)+disy);
		    	gl.glVertex2f(x,y);
			}
		}
		else
		{
			double minX =location.x;
			double minY = location.y;
			double maxX = location.x+PuzzlePiece.size;
			double maxY = location.y+PuzzlePiece.size;
			for (double theta = 0; theta < TWO_PI; theta += segment) {
			    float x = (float) (coord.x + (Math.sin(theta) * radius)+disx);
			    float y = (float) (coord.y + (Math.cos(theta) * radius)+disy);
			    if((x <= maxX && x >= minX) && (y >= minY && y <= maxY))
			    {
			    	gl.glVertex2f(x,y);
			    }
			}
		}
		gl.glEnd();
	}
	/**
	 * a clone method that clones the same shape in a different spot
	 * the position is determined by coord coordinate
	 * @param invert
	 * @return
	 */
	public PuzzleCut clone(boolean bool)
	{
		PuzzleCut result =new RoundCut(size, displacement,inverted);
		if(bool)
				result.invert();

		return result;
	}
}
